﻿//连接数据库
var config = {
    host: 'localhost',
    user: 'root',
    password: '123456',
    database:'test'
};
var dbtool = require('./database/DbTool.js');
//增加
var  userAddSql = 'INSERT INTO user(name,password) VALUES(?,?)';
var  userAddSql_Params = ['haha', 'test'];
//增 add
dbtool.query(config,userAddSql,userAddSql_Params,function (err, result) {
        if(err){
         console.log('[INSERT ERROR] - ',err.message);
         return;
        }       
       console.log('-------INSERT----------');  
       console.log('INSERT ID:',result);       
       console.log('-------INSERT END----------');
});
//修改
var userModSql = 'UPDATE user SET password = ? WHERE name = ?';
var userModSql_Params = ['test1','haha'];
//改 up
dbtool.query(config,userModSql,userModSql_Params,function (err, result) {
   if(err){
         console.log('[UPDATE ERROR] - ',err.message);
         return;
   }       
  console.log('----------UPDATE-------------');
  console.log('UPDATE affectedRows',result.affectedRows);
  console.log('----------UPDATE END-------------');
});
//查询
dbtool.query(config,'select * from user', function(err, result) {
    if (err) throw err;
	console.log('---------------SELECT----------------');
    console.log('查询结果为: ', result);
	console.log('---------------SELECT END----------------');
});
//删除
var  userDelSql = "DELETE FROM user WHERE name = ?";
var  userDelSql_Params = ['haha'];
dbtool.query(config,userDelSql,userDelSql_Params,function (err, result) {
        if(err){
          console.log('[DELETE ERROR] - ',err.message);
          return;
        }
       console.log('-------------DELETE--------------');
       console.log('DELETE affectedRows',result.affectedRows);
       console.log('-------------DELETE END--------------');
});
//删除所有数据
var  userDelSql = "DELETE FROM user ";
var  userDelSql_Params = [];
dbtool.query(config,userDelSql,userDelSql_Params,function (err, result) {
    if(err){
        console.log('[DELETE ERROR] - ',err.message);
        return;
    }
    console.log('-------------DELETE--------------');
    console.log('DELETE affectedRows',result.affectedRows);
    console.log('-------------DELETE END--------------');
});